<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>WeakSet</title>
</head>

<body>

</body>
<script>
  function excuteRecursive(fn, subject, refs = new WeakSet()) {
    if (refs.has(subject)) return
    fn(subject)
    if (typeof subject === 'object') {
      refs.add(subject);
      for (let key in subject) {
        const child = subject[key];
        excuteRecursive(fn, child, refs);
      }
    }
    console.log(refs.size)
  }

  var foo = {
    foo: 'foo',
    bar: {
      bar: 'Bar'
    }
  }

  foo.baz = foo;
  window.refs = new WeakSet()

  excuteRecursive((sub) => console.log(sub), foo, window.refs)

  console.log(refs.has(foo));
</script>

</html>